﻿Kwik.Page.include("Kwik.Collections.Hashtable.js");
Kwik.Page.include("Kwik.Net.HttpCookie.js");


/** 
* Collection of cookies, loads document.cookie into an 'item' object 
* @constructor 
* @extends Kwik.Collections.Hashtable
* @requires Kwik.Collections.Hashtable
* @requires Kwik.Net.HttpCookie
* @created 12:44 PM 3/5/2007
* @modified 11:41 PM 7/28/2009
*/
Kwik.Net.HttpCookieCollection = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Collections.Hashtable");
	this.__class.push("KWik.Net.HttpCookieCollection");

	
	/** Sets the new cookie or overwrites an existing @type Kwik.Net.HttpCookie */
	this.add = function HttpCookieCollection_setCookie(cookie)
	{
		document.cookie = cookie.toString();
		this.Kwik$Collections$Hashtable.add(cookie.name, cookie);
	};

	/** Remove a cookie */
	this.remove = function HttpCookieCollection_remove(name)
	{
		var cookie = this.Kwik$Collections$Hashtable.remove(cookie.name, cookie);
		cookie.value = "";
		cookie.expires = new Date(1800, 1, 1);
		document.cookie = cookie.toString();
	};

	/** Remove all cookies from the collection @type Kwik.Net.HttpCookie[] */
	this.removeAll = function()
	{
		var cookies = this.Kwik$Collections$Hashtable.removeAll();
		for(var i=0; i<cookies.length; i++)
		{
			cookies[i].value = "";
			cookies[i].expires = new Date(1800, 1, 1);
			document.cookie = cookies[i].toString();
		}
		return cookies;
	};

	/** Get the integer value of a cookie @type Boolean */
	this.getInt = function(key)
	{
		var ret = this.getItem(key, null);
		ret = ret != null? parseInt(ret.value): 0;
		return isNaN(ret)? 0: ret;
	};

	/** Get the float value of a cookie @type Boolean */
	this.getFloat = function(key)
	{
		var ret = this.getItem(key, null);
		ret = ret != null? parseFloat(ret.value): 0;
		return isNaN(ret)? 0: ret;
	};

	/** Get the boolean value of a cookie @type Boolean */
	this.getBoolean = function(key)
	{
		var ret = this.getItem(key, null);
		if(ret == null){return false;}
		if(ret.value == "true" || this.getInt(key) != 0){return true;}
		return false;
	};

	/** Clone this cookie collection @type Kwik.Net.HttpCookieCollection */
	this.clone = function()
	{
		return new Kwik.Net.HttpCookieCollection({items:this.items});
	};

	/** @ignore */
	this.main = function HttpCookieCollection_main(config)
	{
		var cookies = document.cookie.split(";");
		for(var i=0; i<cookies.length; i++)
		{
			var name = cookies[i].substr(0, cookies[i].indexOf("="));
			var value = cookies[i].substr(cookies[i].indexOf("=") + 1);
			this.items[name] = new Kwik.Net.HttpCookie({name: name, value: unescape(value)});
		};
		if(config){this.configure(config);}
	}; this.main(config);
};